﻿#region
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Newtonsoft.Json;
using System.Runtime.Serialization.Formatters.Binary;
using System.Web.Script.Serialization;
using System.Runtime.Serialization.Json;
using DataType.MessageType;
#endregion

namespace WargameForDistribution
{
    public static class Utils
    {

        static Utils()
        {
            JsonConvert.DefaultSettings = () => new JsonSerializerSettings
            {
                DateTimeZoneHandling = DateTimeZoneHandling.Utc
            };
        }
        /// <summary>
        ///     序列化Json字符串
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string SerializeJsonData(object obj)
        {
            var responseData = JsonConvert.SerializeObject(obj);
            return responseData;
        }

        public static T DeserializeJsonData<T>(string josnText)
        {
            if (string.IsNullOrEmpty(josnText))
            {
                return default(T);
            }

            var data = JsonConvert.DeserializeObject<T>(josnText);
            return data;
        }
        #region 普通意义上的字符串和bytes的转换（在一个使用上下文或者相似的语境中可以）

        public static byte[] GetBytes(string str)
        {
            var bytes = new byte[str.Length * sizeof(char)];
            Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
            return bytes;
        }

        public static string GetString(byte[] bytes)
        {
            var chars = new char[bytes.Length / sizeof(char)];
            Buffer.BlockCopy(bytes, 0, chars, 0, bytes.Length);
            return new string(chars);
        }

        #endregion
        public static bool serializeObjToStr(Object obj, string serializedStr)
        {
            bool serializeOk = false;
            serializedStr = "";
            try
            {
                MemoryStream memoryStream = new MemoryStream();
                BinaryFormatter binaryFormatter = new BinaryFormatter();
                binaryFormatter.Serialize(memoryStream, obj);
                serializedStr = System.Convert.ToBase64String(memoryStream.ToArray());

                serializeOk = true;
            }
            catch
            {
                serializeOk = false;
            }

            return serializeOk;
        }
        public static List<ActionMsg> Parse(string s_js)
        {
            List<ActionMsg> actions = new List<ActionMsg>();
            if (s_js == "")
                return actions;
            else
            {
                JavaScriptSerializer jss = new JavaScriptSerializer();
                actions = jss.Deserialize<List<ActionMsg>>(s_js);
                //string[] action_array = s_js.Split(',');
                //for (int i = 0; i < action_array.Count(); i++)
                //{
                //    JavaScriptSerializer jss = new JavaScriptSerializer();
                //    ActionMsg a = jss.Deserialize<ActionMsg>(action_array[i]);

                //    //将接收到的action加入列表中 
                //    actions.Add(a);
                //}
                return actions;
            }
        }
        public static StreamReader StructToJson(Object obj)
        {

            MemoryStream stream1 = new MemoryStream();
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(ObservationMsg));
            ser.WriteObject(stream1, obj);
            stream1.Position = 0;
            StreamReader sr = new StreamReader(stream1);
            return sr;
        }
    }
}
